package com.fuyun.microservice.handler;


import com.fuyun.microservice.codec.PacketType;
import com.fuyun.microservice.codec.msg.Header;
import com.fuyun.microservice.codec.msg.Message;
import com.fuyun.microservice.connect.BroadMessageAdaptor;
import io.netty.channel.ChannelHandlerContext;

/**
 * 广播消息确认
 */
public class BroadcastAckHandler  extends AuthAbstractHandler<Message>  {

    private final BroadMessageAdaptor broadMessageAdaptor;

    public BroadcastAckHandler(BroadMessageAdaptor broadMessageAdaptor){
        this.broadMessageAdaptor=broadMessageAdaptor;
    }


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
        channelAuth(ctx,msg);
        Header header=msg.getHeader();
        if(header.getCommandId()!= PacketType.BROADRESPONSE.getCommandId()){
            ctx.fireChannelRead(msg);
        }

        broadMessageAdaptor.acknowledge(ctx.channel(),header.getSequenceNo());
    }
}
